<template>
  <view v-if="goodsInfo.goods_name" class="goods-detail-container">
    <!-- 轮播图 -->
    <swiper :indicator-dots="true" :interval="3000" :duration="1000"  :autoplay="true"   :circular="true">
      <swiper-item v-for="(item, i) in goodsInfo.pics" :key="i">
        <image :src="item.pics_big" @click="preview(i)"></image>
      </swiper-item>
    </swiper>
    
    <!-- //商品信息区域 -->
    <view class="goods-info-box">
      <!-- 价格 -->
      <view class="goods-price">¥ {{goodsInfo.goods_price}}</view>
      <!-- 主信息区域 -->
      <view class="main-info">
        <view class="info-left">{{goodsInfo.goods_name}}</view>
        <view class="icon-right">
          <uni-icons type="star" size="18" color="gray"></uni-icons>
          <view class="sc">收藏</view>
        </view>
      </view>
      <!-- 快递信息 -->
      <view class="mini-info">快递：免运费</view>
    </view>
  <!--富文本  -->
    <rich-text :nodes="goodsInfo.goods_introduce"></rich-text>
    <!-- 底部商品导航 组件-->
    <view class="goods-nav">
       <uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup"  @click="onClick" @buttonClick="buttonClick" />
    </view>
  </view>
</template>

<script>
  //导入辅助函数，访问模块里的数据
  import {mapState, mapMutations, mapGetters} from 'vuex'
  export default {
    computed: {
      //'cart'模块名称，['cart']模块下的数据
      ...mapState('cart', ['cart']),
      ...mapGetters('cart', ['total'])
    },
    //监听数量变化，并将新值赋值给info
    watch: {
      // total(newValue) {
      //   const re = this.options.find(x => x.text === '购物车')
      //   if(re) {
      //     re.info = newValue
      //   }
      // }
      total: {
        handler(newValue) {
          const re = this.options.find(x => x.text === '购物车')
            if(re) {
              re.info = newValue
            }
        },
        immediate: true
      }
    },
    data() {
      return {
        goodsInfo: {},
        options: [{
        			icon: 'shop',
        			text: '店铺',
        			infoBackgroundColor:'#007aff',
        			infoColor:"red"
        		}, {
        			icon: 'cart',
        			text: '购物车',
        			info: 0
        		}],
        	    buttonGroup: [{
        	      text: '加入购物车',
        	      backgroundColor: '#ff0000',
        	      color: '#fff'
        	    },
        	    {
        	      text: '立即购买',
        	      backgroundColor: '#ffa200',
        	      color: '#fff'
        	    }
        	    ],
      }
    },
    methods: {
      //调用购物车模块中的方法
      ...mapMutations('cart', ['addToCart']),
      
      async getGoodsInfo (goods_id) {
        const {data: res} = await uni.$http.get('/api/public/v1/goods/detail', {goods_id})
        if(res.meta.status !== 200) return uni.$showMsg()
        //解决图片有间隙的问题,后缀名为webp的图片在ios上不能显示，需换成jpg
        res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g, '<img style="display: block;"').replace(/webp/g, 'jpg')
        this.goodsInfo = res.message
      },
      //轮播图预览
      preview (i) {
        uni.previewImage({
          current: i,
          //所有图片地址
          urls: this.goodsInfo.pics.map(x => x.pics_big)
        })
      },
      //跳转到tabbar 购物车页面
      onClick (e) {
        // console.log(e);
        if(e.content.text === '购物车') {
          uni.switchTab({
            url:'/pages/cart/cart'
          })
        }
      },
      
      //点击加入购物车按钮
      buttonClick (e) {
        if(e.content.text === '加入购物车') {
          //准备信息对象
          const goods = {
             goods_id: this.goodsInfo.goods_id,       // 商品的Id
             goods_name: this.goodsInfo.goods_name,   // 商品的名称
             goods_price: this.goodsInfo.goods_price, // 商品的价格
             goods_count: 1,                           // 商品的数量
             goods_small_logo: this.goodsInfo.goods_small_logo, // 商品的图片
             goods_state: true                         // 商品的勾选状态
          }
          //调用方法,并传参
          this.addToCart(goods)
          
        }
      }
    },
    onLoad (options) {
       const goods_id = options.goods_id
      this.getGoodsInfo (goods_id)
      
    },
  }
</script>

<style lang="scss">
  swiper {
    height: 750rpx;
    image {
      width: 100%;
      height: 100%;
    }
  }
  .goods-info-box {
    padding: 10px;
    padding-right: 0;
    .goods-price {
      font-size: 18px;
      color: #c00000;
      margin: 10px 0;
    }
    .main-info {
      display: flex;
      justify-content: space-between;
      .info-left {
        padding-right: 10px;
          font-size: 13px;
          font-weight: bold;
      }
      .icon-right {
        width: 120px;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        font-size: 12px;
        border-left: 1px solid #efefef;
        color: gray;
      }
    }
    .mini-info {
        font-size: 12px;
        margin: 10px 0;
        color: grey;
    }
  }
  .goods-detail-container {
    // 给页面外层的容器，添加 50px 的内padding，
    // 防止页面内容被底部的商品导航组件遮盖
    padding-bottom: 50px;
  }
  .goods-nav {
    //固定在底部
     position: fixed;
      bottom: 0;
      left: 0;
      width: 100%;
  }

</style>
